package com.cmd;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.player.User;
import com.qy.util.RedisPoolMgr;
import com.qy.util.SMSUtil;
import com.qy.util.StaticData;
import com.util.AbstractCommand;
import com.util.PBMessage;

public class SendMsgCMD extends AbstractCommand{

	@Override
	public void execute(User user, PBMessage packet) throws Exception {
		/*
		 * 获取用户手机号
		 * 然后生成随机码发送
		 */
		JSONObject reJs = new JSONObject();
		JSONObject param = packet.getMessage();
		if(!param.containsKey("phone")){
		    reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
		    user.sendPacket(reJs);
		    return;
		}
		Object tmp =  RedisPoolMgr.hashGet(StaticData.REDISKEY.V_CODE_ERROR.toString(), user.getKey());
		int count = 0;
		if(tmp!=null&&!tmp.equals("null")){
		    count = ((int) tmp)+1;
		}
		if(count>=5){
		    reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.user_lock.getType());
		    user.sendPacket(reJs);
		    return;
		}
		String phone = param.getString("phone");
		 Map<String,Object> modelMap = SMSUtil.sendCode(phone);
		String reCode =  modelMap.get("code").toString();
		/*
		 * 发送失败
		 */
		if(!reCode.equals("send_success")){
			reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.SEND_ERROR.getType());
			user.sendPacket(reJs);
			return;
		}
		/*
		 * 成功则添加到redis中
		 */
		RedisPoolMgr.set(user.getKey()+phone, modelMap.get("smscode"),2*60);
		reJs.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		reJs.put(StaticData.reKey.cmd.getKey(),packet.getCode());
		user.sendPacket(reJs);
	}

}
